package com.ruoyi.web.controller.jl;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.Result;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.jl.domain.Vip;
import com.ruoyi.jl.service.IVipService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * vip等级 控制层
 * 
 * @author wh
 * @date 2019-04-29
 */
@Slf4j
@Controller
@RequestMapping("/jl/vip")
public class VipController extends BaseController {
    private String prefix = "jl/vip";
	
	private final IVipService vipService;

	@Autowired
	public VipController(IVipService vipService) {
		this.vipService = vipService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("jl:vip:view")
	@GetMapping
	public String vip(){
	    return prefix + "/vip";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap){
		Vip vip = vipService.getById(id);
		mmap.put("vip", vip);
		return prefix + "/edit";
	}
	
	/**
	 * 查询vip等级列表
	 */
	@RequiresPermissions("jl:vip:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(Vip vip){
		startPage();
        List<Vip> list = vipService.list(vip);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存vip等级
	 */
	@RequiresPermissions("jl:vip:add")
	@Log(title = "vip等级", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(Vip vip){
		vipService.save(vip);
		return Result.success();
	}

	/**
	 * 修改保存vip等级
	 */
	@RequiresPermissions("jl:vip:edit")
	@Log(title = "vip等级", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(Vip vip){
		vipService.updateById(vip);
		return Result.success();
	}
	
	/**
	 * 删除vip等级
	 */
	@RequiresPermissions("jl:vip:remove")
	@Log(title = "vip等级", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		vipService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出vip等级列表
	 */
	@RequiresPermissions("jl:vip:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(Vip vip){
		List<Vip> list = vipService.list(vip);
		ExcelUtil<Vip> util = new ExcelUtil<>(Vip.class);
		util.exportExcel(list, "vip");
		return Result.success();
	}

	/**
	 * 选择部门树页面
	 */
	@GetMapping("/selectVipTree/{vipId}")
	public String selectGameTree(@PathVariable("vipId") Integer vipId, ModelMap mmap) {
		Vip vip = vipService.selectDefaultTree(vipId);
		mmap.put("vip", vip);
		return prefix + "/tree";
	}

	/**
	 * 加载部门列表树
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData() {
		 List<Map<String, Object>> trees = vipService.treeData();
		 return trees;
	}

}
